{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dd99d5d2",
   "metadata": {},
   "source": [
    "# statistics 统计\n",
    "\n",
    "- norm\n",
    "\n",
    "- mean sum\n",
    "\n",
    "- prod\n",
    "\n",
    "- max, min, argmin, argmax\n",
    "\n",
    "- kthvalue, topk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "16b8ff16",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f459505",
   "metadata": {},
   "source": [
    "### norm 范数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b7443918",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1., 1., 1., 1., 1., 1., 1., 1.])\n",
      "tensor([[1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.]])\n",
      "tensor([[[1., 1.],\n",
      "         [1., 1.]],\n",
      "\n",
      "        [[1., 1.],\n",
      "         [1., 1.]]])\n",
      "tensor(8.) tensor(8.) tensor(8.)\n",
      "tensor(2.8284) tensor(2.8284) tensor(2.8284)\n",
      "tensor([4., 4.])\n",
      "tensor([2., 2.])\n",
      "tensor([[2., 2.],\n",
      "        [2., 2.]])\n",
      "tensor([[1.4142, 1.4142],\n",
      "        [1.4142, 1.4142]])\n"
     ]
    }
   ],
   "source": [
    "# 需要输入浮点数\n",
    "a = torch.full([8], 1.)\n",
    "b = a.view(2, 4)\n",
    "c = a.view(2, 2, 2)\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "\n",
    "# 一范数是x绝对值求和\n",
    "print(a.norm(1), b.norm(1), c.norm(1))\n",
    "\n",
    "# 二范数是x的平方和的平方根\n",
    "print(a.norm(2), b.norm(2), c.norm(2))\n",
    "\n",
    "# 设定dim=1，表示维度1肖掉\n",
    "print(b.norm(1, dim=1))\n",
    "\n",
    "print(b.norm(2, dim=1))\n",
    "\n",
    "print(c.norm(1, dim=0))\n",
    "\n",
    "print(c.norm(2, dim=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6cd86dd",
   "metadata": {},
   "source": [
    "### mean, sum, min, max, prod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f78afc7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0., 1., 2., 3.],\n",
      "        [4., 5., 6., 7.]])\n",
      "tensor(0.) tensor(7.) tensor(3.5000) tensor(0.)\n",
      "tensor(28.)\n",
      "tensor(7) tensor(0)\n"
     ]
    }
   ],
   "source": [
    "a = torch.arange(8).view(2, 4).float()\n",
    "print(a)\n",
    "\n",
    "# mean 均值\n",
    "# prod 累乘\n",
    "print(a.min(), a.max(), a.mean(), a.prod())\n",
    "\n",
    "# 求和\n",
    "print(a.sum())\n",
    "\n",
    "# argmax 最大值的索引值\n",
    "# argmin 最小值的索引值\n",
    "print(a.argmax(), a.argmin())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d4cc8050",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[0., 1., 2., 3.],\n",
      "         [4., 5., 6., 7.]]])\n",
      "tensor(7)\n",
      "tensor(0)\n",
      "tensor(20)\n"
     ]
    }
   ],
   "source": [
    "# argmin, argmax 举例\n",
    "a = torch.arange(8).view(2, 4).float()\n",
    "\n",
    "a = a.view(1, 2, 4)\n",
    "print(a)\n",
    "\n",
    "print(a.argmax())\n",
    "\n",
    "print(a.argmin())\n",
    "\n",
    "a = torch.rand(2, 3, 4)\n",
    "print(a.argmax())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f656db26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([-0.1674,  0.1962, -1.7006,  0.9202,  2.7241,  1.1844,  1.1609,  0.1750,\n",
      "        -0.4118,  1.1225])\n",
      "tensor(4)\n",
      "tensor([4, 0, 9, 7])\n"
     ]
    }
   ],
   "source": [
    "a = torch.randn(4, 10)\n",
    "print(a[0])\n",
    "\n",
    "print(a.argmax())\n",
    "\n",
    "print(a.argmax(dim=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b43663c2",
   "metadata": {},
   "source": [
    "### dim, keepdim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a5a18322",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 10])\n",
      "tensor([[-0.1674,  0.1962, -1.7006,  0.9202,  2.7241,  1.1844,  1.1609,  0.1750,\n",
      "         -0.4118,  1.1225],\n",
      "        [ 1.5984, -0.3630,  0.3823, -0.8169, -2.7389, -0.2661, -0.1359, -0.0316,\n",
      "          0.5870,  0.4733],\n",
      "        [-0.1589, -0.5152, -1.7262,  0.6181, -0.1019,  0.0489,  1.2201, -1.0256,\n",
      "          0.3471,  1.4853],\n",
      "        [ 0.8250,  0.3299, -0.3429, -1.6535, -0.0137,  0.1097,  0.6989,  1.2909,\n",
      "          1.2478,  0.8429]])\n",
      "torch.return_types.max(\n",
      "values=tensor([2.7241, 1.5984, 1.4853, 1.2909]),\n",
      "indices=tensor([4, 0, 9, 7]))\n",
      "tensor([4, 0, 9, 7])\n",
      "torch.return_types.max(\n",
      "values=tensor([[2.7241],\n",
      "        [1.5984],\n",
      "        [1.4853],\n",
      "        [1.2909]]),\n",
      "indices=tensor([[4],\n",
      "        [0],\n",
      "        [9],\n",
      "        [7]]))\n",
      "tensor([[4],\n",
      "        [0],\n",
      "        [9],\n",
      "        [7]])\n"
     ]
    }
   ],
   "source": [
    "print(a.shape)\n",
    "print(a)\n",
    "\n",
    "# （二维）每一列的最大值\n",
    "# 返回置信度、索引值\n",
    "print(a.max(dim=1))\n",
    "\n",
    "print(a.argmax(dim=1))\n",
    "\n",
    "# keepdim=True 求指定维度的最大值会肖去这个维度，keepdim=True的作用是保留这个维度，为1\n",
    "# 如：[4, 10].max(dim=1, keepdim=True)  得到[4, 1]\n",
    "print(a.max(dim=1, keepdim=True))\n",
    "\n",
    "print(a.argmax(dim=1, keepdim=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "450d5693",
   "metadata": {},
   "source": [
    "### Top-k or k-th 最大的几个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b5b0a01d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1.2402, -1.2858,  0.3718, -1.2007, -0.5725, -0.2768,  0.9669,  0.6666,\n",
      "         -0.9188,  2.0089],\n",
      "        [ 0.5285,  0.7384, -0.8231,  0.8419,  2.1691, -0.2844,  0.1454,  1.2405,\n",
      "         -1.6356, -0.2808],\n",
      "        [-0.4659, -0.5187,  0.8322, -0.5436, -0.9430,  1.0654,  1.1466, -0.5739,\n",
      "          0.2257,  1.6100],\n",
      "        [-0.9225,  0.0265, -0.7137, -0.8992, -0.4856, -0.4490,  0.2059, -1.6328,\n",
      "         -0.7133, -0.6122]])\n",
      "torch.return_types.topk(\n",
      "values=tensor([[ 2.0089,  1.2402,  0.9669],\n",
      "        [ 2.1691,  1.2405,  0.8419],\n",
      "        [ 1.6100,  1.1466,  1.0654],\n",
      "        [ 0.2059,  0.0265, -0.4490]]),\n",
      "indices=tensor([[9, 0, 6],\n",
      "        [4, 7, 3],\n",
      "        [9, 6, 5],\n",
      "        [6, 1, 5]]))\n",
      "torch.return_types.topk(\n",
      "values=tensor([[-1.2858, -1.2007, -0.9188],\n",
      "        [-1.6356, -0.8231, -0.2844],\n",
      "        [-0.9430, -0.5739, -0.5436],\n",
      "        [-1.6328, -0.9225, -0.8992]]),\n",
      "indices=tensor([[1, 3, 8],\n",
      "        [8, 2, 5],\n",
      "        [4, 7, 3],\n",
      "        [7, 0, 3]]))\n",
      "torch.return_types.kthvalue(\n",
      "values=tensor([ 0.9669,  0.8419,  1.0654, -0.4490]),\n",
      "indices=tensor([6, 3, 5, 5]))\n",
      "torch.return_types.kthvalue(\n",
      "values=tensor([-0.9188, -0.2844, -0.5436, -0.8992]),\n",
      "indices=tensor([8, 5, 3, 3]))\n",
      "torch.return_types.kthvalue(\n",
      "values=tensor([-0.9188, -0.2844, -0.5436, -0.8992]),\n",
      "indices=tensor([8, 5, 3, 3]))\n"
     ]
    }
   ],
   "source": [
    "a = torch.randn(4, 10)\n",
    "print(a)\n",
    "\n",
    "# 第一维度的top 3\n",
    "print(a.topk(3, dim=1))\n",
    "\n",
    "# largest=False 求最小的几个\n",
    "print(a.topk(3, dim=1, largest=False))\n",
    "\n",
    "# 第8小的（第3大）\n",
    "print(a.kthvalue(8, dim=1))\n",
    "\n",
    "print(a.kthvalue(3))\n",
    "\n",
    "print(a.kthvalue(3, dim=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9cdc0df",
   "metadata": {},
   "source": [
    "### compare 比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "25f7bbcf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1.2402, -1.2858,  0.3718, -1.2007, -0.5725, -0.2768,  0.9669,  0.6666,\n",
      "         -0.9188,  2.0089],\n",
      "        [ 0.5285,  0.7384, -0.8231,  0.8419,  2.1691, -0.2844,  0.1454,  1.2405,\n",
      "         -1.6356, -0.2808],\n",
      "        [-0.4659, -0.5187,  0.8322, -0.5436, -0.9430,  1.0654,  1.1466, -0.5739,\n",
      "          0.2257,  1.6100],\n",
      "        [-0.9225,  0.0265, -0.7137, -0.8992, -0.4856, -0.4490,  0.2059, -1.6328,\n",
      "         -0.7133, -0.6122]])\n",
      "tensor([[ True, False,  True, False, False, False,  True,  True, False,  True],\n",
      "        [ True,  True, False,  True,  True, False,  True,  True, False, False],\n",
      "        [False, False,  True, False, False,  True,  True, False,  True,  True],\n",
      "        [False,  True, False, False, False, False,  True, False, False, False]])\n",
      "tensor([[ True, False,  True, False, False, False,  True,  True, False,  True],\n",
      "        [ True,  True, False,  True,  True, False,  True,  True, False, False],\n",
      "        [False, False,  True, False, False,  True,  True, False,  True,  True],\n",
      "        [False,  True, False, False, False, False,  True, False, False, False]])\n",
      "tensor([[True, True, True, True, True, True, True, True, True, True],\n",
      "        [True, True, True, True, True, True, True, True, True, True],\n",
      "        [True, True, True, True, True, True, True, True, True, True],\n",
      "        [True, True, True, True, True, True, True, True, True, True]])\n",
      "tensor([[False, False, False],\n",
      "        [False, False, False]])\n",
      "tensor([[True, True, True],\n",
      "        [True, True, True]])\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a)\n",
    "\n",
    "print(a>0)\n",
    "\n",
    "print(torch.gt(a, 0))\n",
    "\n",
    "print(a != 0)\n",
    "\n",
    "a = torch.ones(2, 3)\n",
    "b = torch.randn(2, 3)\n",
    "# 各个位置的值是否相等\n",
    "print(torch.eq(a, b))\n",
    "\n",
    "print(torch.eq(a, a))\n",
    "\n",
    "print(torch.equal(a, a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85781628",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
